package com.lanou3g.dllo.baidumusic.musiclibrary.mv;

import android.content.Context;
import android.media.Image;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.lanou3g.dllo.baidumusic.R;
import com.lanou3g.dllo.baidumusic.tools.MyRvOnClickListener;
import com.lanou3g.dllo.baidumusic.tools.SingleVolley;

import java.util.List;

/**
 * Created by dllo on 16/6/29.
 */
public class MvAdapter extends RecyclerView.Adapter<MvAdapter.MyViewHolder>{

    private MVBean mvBean;
    private Context context;
    private List<MVBean.ResultBean.MvListBean> list;
    private MyRvOnClickListener myRvOnClickListener;

    public void setMyRvOnClickListener(MyRvOnClickListener myRvOnClickListener) {
        this.myRvOnClickListener = myRvOnClickListener;
    }

    public void addMvBean(MVBean mvBean) {
        list.addAll(mvBean.getResult().getMv_list());
        notifyDataSetChanged();
    }

    public MVBean getMvBean() {
        return mvBean;
    }

    public MvAdapter(Context context) {
        this.context = context;
    }

    public void setMvBean(MVBean mvBean) {
        this.mvBean = mvBean;
        list = mvBean.getResult().getMv_list();
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_mv,parent,false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {

        holder.mvSongsName.setText(list.get(position).getTitle());
        holder.mvAuthor.setText(list.get(position).getArtist());
        SingleVolley.getSingleVolley(context).getImageLoader().get(list.get(position).getThumbnail2(),
                ImageLoader.getImageListener(holder.mvIv,R.mipmap.ic_launcher,R.mipmap.ic_launcher));
        holder.mvLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 当itemView被点击的时候
                // getLayoutPosition是用来获得当前点击的position
                int position = holder.getLayoutPosition();
                // 调用接口对象的onClick方法
                myRvOnClickListener.onClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0: list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView mvIv;
        TextView mvSongsName,mvAuthor;
        LinearLayout mvLayout;

        public MyViewHolder(View itemView) {
            super(itemView);
            mvIv = (ImageView) itemView.findViewById(R.id.mv_iv);
            mvSongsName = (TextView) itemView.findViewById(R.id.mv_songs_name);
            mvAuthor = (TextView) itemView.findViewById(R.id.mv_author);
            mvLayout = (LinearLayout) itemView.findViewById(R.id.mv_layout);
        }
    }
}
